// ==UserScript==
// @name         在线考试样式优化插件
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  优化在线考试页面样式，隐藏干扰元素，统一字体，调整布局
// @author       Inscode AI
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 配置对象
    const config = {
        // 需要隐藏的元素选择器
        hideSelectors: [
            '.header',
            '.bread-nav',
            '.tools',
            '.seal-line',
            '.fiexd-nav',
            '.footer',
            '.exam-header',
            '.nav-bar',
            '.toolbar',
            '.page-footer'
        ],
        
        // 字体配置
        fonts: {
            chinese: '宋体',
            english: 'Times New Roman'
        },
        
        // 样式开关
        enabled: true
    };

    // 创建样式标签
    function createStyleSheet() {
        const style = document.createElement('style');
        style.id = 'exam-style-optimizer';
        style.textContent = `
            /* 全局字体设置 */
            * {
                font-family: "${config.fonts.chinese}", sans-serif;
            }

            /* 英文和数字字体 */
            table tr td,
            .question-number,
            .score,
            .time,
            .exam-info {
                font-family: "${config.fonts.english}", serif !important;
            }

            /* 标题加粗 */
            .main-title,
            .questypename {
                font-weight: bold;
            }

            /* 题目内容字体 */
            .tk-quest-item .exam-item__cnt {
                font-family: "${config.fonts.chinese}" !important;
            }

            /* 隐藏不需要的元素 */
            ${config.hideSelectors.join(', ')} {
                display: none !important;
            }

            /* 调整试卷容器宽度 */
            .group-exam-page > .paper-cnt,
            .group-exam-page .paper-body .ques-item {
                max-width: 100% !important;
                width: 100% !important;
            }

            /* 调整内边距 */
            .group-exam-page > .paper-cnt {
                padding-top: 0.25rem !important;
                margin-right: 0.25rem !important;
            }

            .group-exam-page > .paper-cnt > .paper-main {
                padding: 0 0.5rem 0 0.5rem !important;
            }

            .group-exam-page .paper-body .ques-item {
                padding: 0 0.625rem 0 1.25rem !important;
                margin-bottom: 0 !important;
            }

            .tk-quest-item .wrapper {
                padding: 0.25rem 0.5rem 0.25rem 0.5rem !important;
            }

            /* 题目类型间距 */
            .group-exam-page .paper-body .paper-part .part-body .ques-type {
                margin-bottom: 0 !important;
            }

            /* 移除悬停效果 */
            .group-exam-page .paper-body .ques-item:hover > .ques-ctrl,
            .group-exam-page .paper-body .ques-item:hover::after,
            .group-exam-page .paper-body .paper-part .part-body .ques-item:hover {
                box-shadow: none !important;
                border: none !important;
                background: transparent !important;
            }

            /* 优化题目间距 */
            .ques-item {
                margin-bottom: 1rem !important;
            }

            /* 优化题目编号样式 */
            .question-number {
                font-weight: bold;
                margin-right: 0.5rem;
            }

            /* 响应式设计 */
            @media screen and (max-width: 768px) {
                .group-exam-page > .paper-cnt {
                    padding: 0.5rem !important;
                    margin: 0 !important;
                }
                
                .group-exam-page .paper-body .ques-item {
                    padding: 0 0.5rem !important;
                }
            }
        `;
        return style;
    }

    // 创建控制面板
    function createControlPanel() {
        const panel = document.createElement('div');
        panel.id = 'exam-optimizer-panel';
        panel.innerHTML = `
            <div style="
                position: fixed;
                top: 10px;
                right: 10px;
                z-index: 9999;
                background: #fff;
                border: 1px solid #ddd;
                border-radius: 4px;
                padding: 10px;
                box-shadow: 0 2px 8px rgba(0,0,0,0.1);
                font-family: Arial, sans-serif;
                font-size: 12px;
            ">
                <label style="display: flex; align-items: center; cursor: pointer;">
                    <input type="checkbox" id="exam-optimizer-toggle" ${config.enabled ? 'checked' : ''}>
                    <span style="margin-left: 5px;">启用样式优化</span>
                </label>
            </div>
        `;
        
        document.body.appendChild(panel);
        
        // 添加开关事件
        const toggle = document.getElementById('exam-optimizer-toggle');
        toggle.addEventListener('change', function(e) {
            config.enabled = e.target.checked;
            toggleOptimizer();
        });
    }

    // 切换优化器状态
    function toggleOptimizer() {
        const style = document.getElementById('exam-style-optimizer');
        if (style) {
            style.disabled = !config.enabled;
        }
        
        // 显示/隐藏被隐藏的元素
        if (!config.enabled) {
            const hiddenElements = document.querySelectorAll(config.hideSelectors.join(', '));
            hiddenElements.forEach(el => {
                el.style.display = '';
            });
        }
    }

    // 初始化函数
    function init() {
        // 等待DOM加载完成
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', init);
            return;
        }

        // 添加样式
        const style = createStyleSheet();
        document.head.appendChild(style);

        // 添加控制面板
        createControlPanel();

        // 监听URL变化，适用于单页应用
        let lastUrl = location.href;
        new MutationObserver(() => {
            const url = location.href;
            if (url !== lastUrl) {
                lastUrl = url;
                // 重新应用样式
                setTimeout(() => {
                    const style = document.getElementById('exam-style-optimizer');
                    if (style) {
                        style.remove();
                        document.head.appendChild(createStyleSheet());
                    }
                }, 100);
            }
        }).observe(document, { subtree: true, childList: true });
    }

    // 启动优化器
    init();

    // 导出API供控制台使用
    window.ExamStyleOptimizer = {
        toggle: () => {
            const toggle = document.getElementById('exam-optimizer-toggle');
            if (toggle) {
                toggle.click();
            }
        },
        enable: () => {
            config.enabled = true;
            toggleOptimizer();
        },
        disable: () => {
            config.enabled = false;
            toggleOptimizer();
        }
    };
})();
